package com.test4;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSON;
import com.zip.FileToZip;
import com.zip.UnZipUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.charset.Charset;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ReadZip {

    Logger log = LoggerFactory.getLogger(ReadZip.class);

    public List<String> names = ReadText.read("C:/Users/domains90/Desktop/123.txt");

    public String unZipPath = "D:/home/zip/codesunzip/";

    public  void read(String filepath){

        File zip = new File(filepath);
        if(!zip.exists()) {
            throw new RuntimeException("不存在目标的文件:"+zip.getPath());
        }

        //解压源目标路劲
        File childUnZipFile = new File(unZipPath);
        //清除该文件夹
        FileToZip.deleteAllFilesOfDir(childUnZipFile);
        //创建该文件夹
        childUnZipFile.mkdirs();

        UnZipUtil.unzip(zip.getPath(),childUnZipFile.getPath());

        File[] files = childUnZipFile.listFiles();
        this.getFile(files);
    }

    public void getFile(File[] files){
        List<String> namess = new LinkedList<>();
        for(File file:files){
            if(file.isFile()){
                String filename = file.getName();

                String name_new =filename.substring(0,filename.lastIndexOf("_"));
                if(names.contains(name_new)){
                    namess.add(name_new);
                    //System.err.println(name_new);
                    //System.err.println(filename+":"+name_new);
                    FileUtil.copy(file,new File(unZipPath+"/target/"+filename),true);
                }

            }else if(file.isDirectory()){
                this.getFile(file.listFiles());
            }
        }
        for(String name:names){
            if(!namess.contains(name)){
                System.err.println(name);
            }
        }

    }


    public static void main(String[] args) {
        new ReadZip().read("C:/Users/domains90/Desktop/坤站村(20230613160334).zip");
    }



}
